package com.otaku.admin.service;

import com.otaku.admin.dao.PermissionMapper;
import com.otaku.admin.dao.RolePermissionMapper;
import com.otaku.admin.dao.UserMapper;
import com.otaku.admin.dao.UserRoleMapper;
import com.otaku.admin.dao.entity.*;
import com.otaku.admin.util.StringTypeConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("permissionService")
public class PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;


    /**
     * 保存
     *
     * @param permission
     * @return
     */
    public boolean addPerm(Permission permission) {
        String permType = permission.getPermType();
        //目录父节点
        if (StringTypeConstant.PERM_TYPE_CATALOG.equals(permType)) {
            permission.setPermLevel(0);
        } else {
            Integer parentId = permission.getParentId();
            Permission byId = permissionMapper.findById(parentId);
            if (byId != null) {
                Integer permLevel = byId.getPermLevel();
                permission.setPermLevel(permLevel + 1);
            }
        }
        int insert = permissionMapper.insert(permission);
        return insert > 0;
    }

    public boolean deletePerm(Permission permission) {
        int i = permissionMapper.deleteById(permission.getId());
        return i > 0;
    }

    public boolean updatePerm(Permission permission) {
        int i = permissionMapper.updateById(permission);
        return i > 0;
    }

    public List<Permission> findPermAll() {
        return permissionMapper.findAllPermission();
    }

    public List<Permission> findPermByUser(String userName) {
        return userMapper.findPermissionByUsername(userName);
    }

    public List<Role> findRoleByUser(String userName) {
        return permissionMapper.findRoleByUserName(userName);
    }

    public boolean addUserRole(UserRole userRole,String userName) {
        SelfUserDetails byUsername = userMapper.findByUsername(userName);
        userRole.setUserId(byUsername.getId());
        List<UserRole> userAndRole = userRoleMapper.findByUserAndRole(userRole);
        int insert = 0;
        if (userAndRole != null && userAndRole.size() > 0) {
            insert = userAndRole.size();
        } else {
            insert = userRoleMapper.insert(userRole);
        }
        return insert > 0;
    }

    public void deleteUserRole(UserRole userRole,String userName) {
        SelfUserDetails byUsername = userMapper.findByUsername(userName);
        userRole.setUserId(byUsername.getId());
        userRoleMapper.deleteByUserAndRole(userRole);
    }

    public boolean addRolePerm(RolePermission rolePermission) {


        List<RolePermission> roleAndPerm = rolePermissionMapper.findByRoleAndPerm(rolePermission);
        int insert = 0;
        if (roleAndPerm != null && roleAndPerm.size() > 0) {
            insert = roleAndPerm.size();
        } else {
            insert = rolePermissionMapper.insert(rolePermission);
        }
        return insert > 0;
    }

    public void deleteRolePerm(RolePermission rolePermission) {
        rolePermissionMapper.deleteByRoleAndPerm(rolePermission);
    }



}
